public class Test {
    public static void main(String[] args) {

    }

    public static void clear() {
        ListNode cur = head;
        while (head != null) {
            cur = head.next;
            head.val = null;
            head.next = null;
            head = cur;
        }
        head = null;
    }

    public static ListNode reverseList() {
        if (head == null) {
            return null;
        }
//只有一个节点
        if (head.next == null) {
            return head;
        }
//
        ListNode cur = head.next;
        while(cur != null){
            cur = head.next;
            head.next = null;
            cur.next = head;
            head = cur;
        }
        return head;
    }

    public ListNode middleNode() {
        ListNode cur = head;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        count = count/2;
        while(count > 0){
            cur = cur.next;
            count--;
        }
        return cur;
    }

    public ListNode findKthToTail(int k) {
        if(k <= 0 || k > size()) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        for(int i=0;i<size() - k ;i++){
            fast = fast.next;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
